package com.mingtu.jgspatrol.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.SPStaticUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.base.BaseBindingFragment;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.utils.SPTools;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.activity.ChargeAreaActivity;
import com.mingtu.jgspatrol.activity.ExchangeActivity;
import com.mingtu.jgspatrol.activity.MyRecordActivity;
import com.mingtu.jgspatrol.activity.RecordActivity;
import com.mingtu.jgspatrol.activity.ReportTypeActivity;
import com.mingtu.jgspatrol.activity.ResultStatisticsActivity;
import com.mingtu.jgspatrol.activity.SendTaskActivity;
import com.mingtu.jgspatrol.activity.TaskListActivity;
import com.mingtu.jgspatrol.adapter.IndexBannerAdapter;
import com.mingtu.jgspatrol.adapter.MenuAdapter;
import com.mingtu.jgspatrol.adapter.TabbarAdapter;
import com.mingtu.jgspatrol.bean.BnnerDataBean;
import com.mingtu.jgspatrol.bean.IndexTaskBean;
import com.mingtu.jgspatrol.bean.MyReportRecordBean;
import com.mingtu.jgspatrol.bean.NoticeBean;
import com.mingtu.jgspatrol.databinding.FragmentIndexBinding;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class IndexFragment extends BaseBindingFragment<FragmentIndexBinding> implements LazyFragmentPagerAdapter.Laziable {
    private SmartRefreshLayout srlUp;
    private static final String[] mTitles = {"待办任务", "单位通知", "上报事件", "消息提醒"};
    private static final int[] mSeleIcons = {R.mipmap.icon_nav1_selected, R.mipmap.icon_nav2_selected, R.mipmap.icon_nav3_selected, R.mipmap.icon_nav4_selected};
    private static final int[] mNormalIcons = {R.mipmap.icon_nav1_normal, R.mipmap.icon_nav2_normal, R.mipmap.icon_nav3_normal, R.mipmap.icon_nav4_normal};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private Nav1Fragment nav1Fragment;
    private Nav2Fragment nav2Fragment;
    private Nav3Fragment nav3Fragment;
    private Nav4Fragment nav4Fragment;
    private int navPosition = 0;
    private int[] currentPageArray = {1, 1, 1, 1};
    private boolean[] isLoadMoreArray = {true, true, true, true};
    private List<IndexTaskBean.PageBean.ListBean> IndexTaskDataBean = new ArrayList<>();
    private List<NoticeBean.PageBean.ListBean> IndexNoticeDataBean = new ArrayList<>();
    private LinkedList<MyReportRecordBean.PageBean.ListBean> IndexReportDataBean = new LinkedList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void initData() {
        //自定义的图片适配器，也可以使用默认的BannerImageAdapter
        IndexBannerAdapter adapter = new IndexBannerAdapter(BnnerDataBean.getData());

        binding.banner.setAdapter(adapter)
//              .setCurrentItem(0,false)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(getActivity()));//设置指示器
        nav1Fragment = new Nav1Fragment();
        nav2Fragment = new Nav2Fragment();
        nav3Fragment = new Nav3Fragment();
        nav4Fragment = new Nav4Fragment();

        mFragments.add(nav1Fragment);
        mFragments.add(nav2Fragment);
        mFragments.add(nav3Fragment);
        mFragments.add(nav4Fragment);


        binding.viewPagerNav.setOffscreenPageLimit(4);//需要设置此值
        binding.viewPagerNav.setAdapter(new TabbarAdapter(getChildFragmentManager(), mFragments));

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mSeleIcons[i], mNormalIcons[i]));
        }

        binding.tabNav.setTabData(mTabEntities);
        binding.tabNav.setOnTabSelectListener(onTabSelectListener);
//        binding.tabNav.showDot(3);

        binding.viewPagerNav.addOnPageChangeListener(onPageChangeListener);
        binding.viewPagerNav.setCurrentItem(0);
    }

    @Override
    protected void initView() {
        //其它
        MenuAdapter menuAdapter = new MenuAdapter(getContext());
        binding.gridView.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(new MenuOnItenClickListner());
        initListener();

    }

    @Override
    protected void getData() {

    }

    private void initListener() {
        initPullRefresh();//刷新
        initLoadMoreListener();
    }


    private void initPullRefresh() {
        srlUp.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                srlUp.setNoMoreData(false);

                if (navPosition == 0) {
                    nav1Fragment.refresh();
                }
                if (navPosition == 1) {
                    nav2Fragment.refresh();
                }
                if (navPosition == 2) {
                    nav3Fragment.refresh();
                }

                if (navPosition == 3) {
                    nav4Fragment.refresh();
                }

            }
        });
    }

    private void initLoadMoreListener() {
        srlUp.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                if (isLoadMoreArray[navPosition]) {
                if (navPosition == 0) {
                    currentPageArray[navPosition] = MyUtills.loadPage(getIndexTaskDataBean(), Constant.PAGE_SIZE);
                    nav1Fragment.getIndexList(currentPageArray[navPosition]);
                }

                if (navPosition == 1) {
                    currentPageArray[navPosition] = MyUtills.loadPage(getIndexNoticeDataBean(), Constant.PAGE_SIZE);
                    nav2Fragment.getNoticeList(currentPageArray[navPosition]);
                }

                if (navPosition == 2) {
                    currentPageArray[navPosition] = MyUtills.loadPage(getIndexReportDataBean(), Constant.PAGE_SIZE);
                    nav3Fragment.getReportList(currentPageArray[navPosition]);
                }

//                }
                srlUp.finishLoadMore(Constant.LOADMORE_TIME);
            }
        });
    }

    private class MenuOnItenClickListner implements MenuAdapter.OnItemClickListener {

        @Override
        public void onItemClick(View view, int position) {
            switch (position) {
                case 0:
                    //责任区
                    IntentUtils.getInstance().readyGo(getActivity(), ChargeAreaActivity.class);
                    break;
                case 1:
                    //上报
                    IntentUtils.getInstance().readyGo(getActivity(), ReportTypeActivity.class);
                    break;
                case 7:
                    //下发
                    IntentUtils.getInstance().readyGo(getActivity(), SendTaskActivity.class);
                    break;
                case 2:
                    //任务单
                    IntentUtils.getInstance().readyGo(getActivity(), TaskListActivity.class);
                    break;
                case 3:
                    //记录单
                    boolean recordAuth = SPStaticUtils.getBoolean(SPTools.recordAuth);
                    if (recordAuth) {
                        IntentUtils.getInstance().readyGo(getActivity(), RecordActivity.class);
                    } else {
                        IntentUtils.getInstance().readyGo(getActivity(), MyRecordActivity.class);
                    }

                    break;
                case 4:
                    //成果单
                    IntentUtils.getInstance().readyGo(getActivity(), ResultStatisticsActivity.class);
                    break;
                case 5:
                    //单位交流
                    Bundle bundle1 = new Bundle();
                    bundle1.putInt("type", 1);
                    IntentUtils.getInstance().readyGo(getActivity(), ExchangeActivity.class, bundle1);
                    break;
                case 6:
                    //学习交流
                    Bundle bundle2 = new Bundle();
                    bundle2.putInt("type", 2);
                    IntentUtils.getInstance().readyGo(getActivity(), ExchangeActivity.class, bundle2);
                    break;
            }

        }
    }

    private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            binding.viewPagerNav.setCurrentItem(position, false);
            navPosition = position;
        }

        @Override
        public void onTabReselect(int position) {

        }
    };

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.tabNav.setCurrentTab(position);
            navPosition = position;
//            binding.viewPagerNav.updateHeight(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    public List<IndexTaskBean.PageBean.ListBean> getIndexTaskDataBean() {
        return IndexTaskDataBean;
    }

    public void setIndexTaskDataBean(List<IndexTaskBean.PageBean.ListBean> indexTaskDataBean) {
        IndexTaskDataBean = indexTaskDataBean;
    }

    public List<NoticeBean.PageBean.ListBean> getIndexNoticeDataBean() {
        return IndexNoticeDataBean;
    }

    public void setIndexNoticeDataBean(List<NoticeBean.PageBean.ListBean> indexNoticeDataBean) {
        IndexNoticeDataBean = indexNoticeDataBean;
    }

    public LinkedList<MyReportRecordBean.PageBean.ListBean> getIndexReportDataBean() {
        return IndexReportDataBean;
    }

    public void setIndexReportDataBean(LinkedList<MyReportRecordBean.PageBean.ListBean> indexReportDataBean) {
        IndexReportDataBean = indexReportDataBean;
    }

    public boolean[] getIsLoadMoreArray() {
        return isLoadMoreArray;
    }

    public void setIsLoadMoreArray(boolean[] isLoadMoreArray) {
        this.isLoadMoreArray = isLoadMoreArray;
    }

}